<template>
  <a-card :bordered="false">

    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="3" :lg="6" :md="6" :sm="24">
            <a-form-item label="入库单号">
              <a-input placeholder="" v-model="queryParam.id"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="4" :lg="6" :md="6" :sm="24">
            <a-form-item label="来源编号">
              <a-input placeholder="" v-model="queryParam.sourceId"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="4" :lg="6" :md="6" :sm="24">
            <a-form-item label="状态">
              <!--状态这里换一下数据字典或自己配置-->
              <j-dict-select-tag v-model="queryParam.status" placeholder="请选择状态" dictCode="inout_status"/>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="6" :md="6" :sm="24">
            <a-form-item label="创建时间">
              <a-range-picker format="YYYY-MM-DD"
                              :placeholder="['开始时间', '结束时间']"
                              @change="onTimeChange"/>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>

        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i>
        <span>已选择</span>
        <a style="font-weight: 600">
          {{ selectedRowKeys.length }}
        </a>
        <span>项</span>
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange,type:type}"
        @change="handleTableChange"
        :customRow="clickThenCheck">

        <span slot="action" slot-scope="text, record">
        <!--
        --   作废：status->1 代入库
        --入库记录：status->7 已完成；status->5 部分出入库
        -->
           <a-popconfirm title="确定作废吗?" @confirm="() => handleDelete(record.id)">
            <a :hidden="[1].indexOf(record.status) === -1">作废</a>
          </a-popconfirm>
          <a-divider :hidden="[1,5,7].indexOf(record.status) === -1" type="vertical"/>
          <a :hidden="[5,7].indexOf(record.status) === -1" @click="handleInRecord(record)">入库记录</a>


        </span>

      </a-table>
    </div>
    <!-- table区域-end -->

    <a-tabs defaultActiveKey="1">
      <a-tab-pane tab="入库明细" :key="refKeys[0]" :forceRender="true">
        <stmStockInItem-ListJX ref="StmStockInItemListJX"></stmStockInItem-ListJX>
      </a-tab-pane>
    </a-tabs>

    <!-- 表单区域 -->
<!--    <stmStockInout-modal ref="modalForm" @ok="modalFormOk"/>-->
    <stm-stock-in-add-modalJX ref="addModalForm" @ok="modalFormOk"/>
    <stm-stock-in-record-modalJX ref="StmStockInRecordModalJX"/>

  </a-card>
</template>

<script>
  import { httpAction, putAction, postAction } from '@/api/manage'
  import StmStockInAddModalJX from './modules/StmStockInAddModalJX'
  import StmStockInRecordModalJX from './modules/StmStockInRecordModalJX'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import StmStockInoutModal from './modules/StmStockInoutModal'
  import { deleteAction } from '@/api/manage'
  import StmStockInItemListJX from './StmStockInItemListJX'
  import StmStockInoutItemModal from './modules/StmStockInoutItemModal'
  import JDate from '@/components/jeecg/JDate'


  export default {
    name: 'StmStockInListJX',
    mixins: [JeecgListMixin],
    components: {
      StmStockInoutModal,
      StmStockInoutItemModal,
      StmStockInItemListJX,
      StmStockInAddModalJX,
      StmStockInRecordModalJX,
      JDate
    },
    data() {
      return {
        refKeys: ['stmStockInoutItem'],
        description: '入库单管理页面',
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key: 'rowIndex',
            width: 60,
            align: 'center',
            customRender: function(t, r, index) {
              return parseInt(index) + 1
            }
          },
          {
            title: '入库单号',
            align: 'center',
            dataIndex: 'id'
          },
          {
            title: '状态',
            align: 'center',
            dataIndex: 'status',
            //1：待出库/待入库，4:已取消，5：部分发货/收货，7：已完成/已出库/已入库
            customRender: function (text) {
              if (text === 1) {
                return '待入库'
              } else if (text === 4) {
                return '已取消'
              } else if (text === 5) {
                return '部分收货'
              }else if (text === 7) {
                return '已入库'
              }
            }
          },
          {
            title: '来源编号',
            align: 'center',
            dataIndex: 'sourceIdStr'
          },
          {
            title: '创建人',
            align: 'center',
            dataIndex: 'createBy'
          },
          {
            title: '创建时间',
            align: 'center',
            dataIndex: 'createTime'
          },
          {
            title: '操作人',
            align: 'center',
            dataIndex: 'updateBy'
          },
          {
            title: '操作时间',
            align: 'center',
            dataIndex: 'updateTime'
          },
          {
            title: '操作',
            dataIndex: 'action',
            align: 'center',
            scopedSlots: { customRender: 'action' }
          }
        ],

        //操作url
        type: 'radio',
        url: {
          list: '/stm/stmStockInout/list',
          delete: '/stm/stmStockInout/cancel',
          confirmBatch: '/stm/stmStockInout/confirmBatch',
        }
      }
    },
    created() {
      this.queryParam.type = 'STOCK_IN'
    },
    methods: {
      clickThenCheck(record) {
        return {
          on: {
            click: () => {
              this.onSelectChange(record.id.toString().split(','), [record])
            }
          }
        }
      },
      onSelectChange(selectedRowKeys, selectionRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectionRows = selectionRows
        this.$refs.StmStockInItemListJX.getMain(this.selectedRowKeys[0])
      },
      onClearSelected() {
        this.selectedRowKeys = []
        this.selectionRows = []
        this.$refs.StmStockInItemListJX.queryParam.mainId = null
        this.$refs.StmStockInItemListJX.loadData()
        this.$refs.StmStockInItemListJX.selectedRowKeys = []
        this.$refs.StmStockInItemListJX.selectionRows = []
      },

      handleDelete: function(id) {
        var that = this
        // 这里调用作废借口
        putAction(that.url.delete, { id: id }).then((res) => {
          if (res.success) {
            that.$message.success(res.message)
            that.loadData()
            this.$refs.StmStockInItemListJX.loadData()
          } else {
            that.$message.warning(res.message)
          }
        })
      },
      onTimeChange: function(value, dateString) {
        console.log(dateString[0], dateString[1])
        this.queryParam.startTime = dateString[0]
        this.queryParam.endTime = dateString[1]
      },
      handleAdd: function() {
        this.$refs.addModalForm.add()
        this.$refs.addModalForm.title = '新增'
      },
      searchQuery: function() {
        this.selectedRowKeys = []
        this.selectionRows = []
        this.$refs.StmStockInItemListJX.queryParam.mainId = null
        this.$refs.StmStockInItemListJX.loadData()
        this.$refs.StmStockInItemListJX.selectedRowKeys = []
        this.$refs.StmStockInItemListJX.selectionRows = []
        this.loadData()
      },
      handleConfirm: function() {
        if (this.selectedRowKeys.length <= 0) {
          this.$message.warning('请选择一条记录！')
          return
        } else {
          let ids = []
          let index = this.selectionRows.findIndex(item => {
            if ( item.status == 4 || item.status == 7) {
              return true;
            }
          })
          if(index !== -1) {
            this.$message.warning('已取消或已完成的单据不可进行出入库操作！')
            return
          }
          for (let a = 0; a < this.selectedRowKeys.length; a++) {
            ids.push(this.selectedRowKeys[a])
          }
          let that = this
          this.$confirm({
            title: '确认出入库操作',
            content: '是否确认选中的出入库单?',
            onOk: function() {
              that.loading = true
              postAction(that.url.confirmBatch, ids ).then((res) => {
                if (res.success) {
                  that.$message.success(res.message)
                  that.loadData()
                  that.onClearSelected()
                } else {
                  that.$message.warning(res.message)
                }
              }).finally(() => {
                that.loading = false
              })
            }
          })
        }
      },
      handleInRecord:function(record){
        this.$refs.StmStockInRecordModalJX.show(record)
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less'
</style>
